<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/8
 * Time: 17:23
 */


namespace app\basketball\controller;

use app\basketball\model\Banner;
use app\basketball\model\BbsCard;
use app\basketball\model\BbsMain;
use app\basketball\model\CardComment;
use app\basketball\model\CollectionM;
use app\basketball\model\Information;
use think\Db;
use think\Exception;
use think\Request;
use think\response\Redirect;

//内容模块
class Content extends BassController{

    /**
     * 首页轮播图
     */
    public function banner_list(){

        $banner_list = Banner::where('dstatus','<>',3)->paginate(10);

        return view('',['banner_list'=>$banner_list]);

    }

    /**
     * 首页轮播图添加
     */
    public function banner_add(){
        if(\request()->isPost()){
            $data = \request()->post();
            $image = uploadImageOne('image');
            if($image['status']==0){
                $data['image'] = $image['message'];
            }
            $res = Banner::create($data);
            if($res){
                $this->success('添加成功','basketball/content/banner_list');
            }else{
                $this->success('添加失败','basketball/content/banner_list');
            }
        }
        return view();
    }

//
//    //论坛社区列表
//    public function forum_list(){
//        $title = request()->get('title');
//        if(empty($title)){
//            $title = '';
//        }
//        $bbs_list = BbsMain::where('dstatus','<>',3)->where('title','like','%'.$title.'%')->order('id desc')->paginate(10);
//
//        return view('',['bbs_list'=>$bbs_list,'title'=>$title]);
//    }
//
//    //添加论坛社区
//    public function forum_add(){
//        if(request()->isPost()){
//            $data = request()->post();
//            $icon = uploadImageOne('icon');
//            $pic_h = uploadImageOne('pic_h');
//            if($icon['status']==0){
//                $data['icon'] = $icon['message'];
//            }
//            if($pic_h['status']==0){
//                $data['pic_h'] = $pic_h['message'];
//            }
//            $status = BbsMain::create($data);
//            if($status){
//                $this->success('添加成功','basketball/content/forum_list');
//            }else{
//                $this->success('添加失败','basketball/content/forum_list');
//            }
//        }
//        return view();
//    }
//
//    //修改论坛社区
//    public function forum_edit(){
//        $id = request()->get('id');
//        $forum_info = BbsMain::get($id);
//        if(request()->isPost()){
//            $data = request()->post();
//            $icon = uploadImageOne('icon');
//            $pic_h = uploadImageOne('pic_h');
//            if($icon['status']==0){
//                $data['icon'] = $icon['message'];
//            }
//            if($pic_h['status']==0){
//                $data['pic_h'] = $pic_h['message'];
//            }
//            $status = $forum_info->save($data,['id'=>$id]);
//            if($status){
//                $this->success('修改成功','basketball/content/forum_list');
//            }else{
//                $this->success('修改失败','basketball/content/forum_list');
//            }
//        }
//
//        return view('',['forum_info'=>$forum_info]);
//    }
//
//    //社区帖子列表
//    public function card_list(){
//        $id = request()->get('id');
//        if(empty($id)){
//            $id = Request::instance()->param('id');
//        }
//        $title = request()->get('title');
//        if(empty($title)){
//            $title = '';
//        }
//        $card_list = BbsCard::where('dstatus',1)->where('bbs_id',$id)->where('title','like','%'.$title.'%')->order('id desc')->paginate(10,false,[ 'query' => ['id'=>$id,'title'=>$title]]);
//
//        return view('',['card_list'=>$card_list,'title'=>$title,'id'=>$id]);
//    }
//
//    /**
//     * 帖子置顶|取消置顶
//     */
//    public function card_top(){
//        //接收帖子id
//        $card_id = Request::instance()->param('id');
//        $bbs_id = Request::instance()->param('bbs_id');
//
//        //查询之前置顶的帖子
//        $card_top_id = BbsCard::where('bbs_id',$bbs_id)->where('rank',1)->value('id');
//        if(!empty($card_top_id)){
//            if($card_top_id!==intval($card_id)){
//                $card = BbsCard::get($card_top_id);
//                $card->rank = 0;
//                $card->save();
//            }
//        }
//        $card_obj = BbsCard::get($card_id);
//        $card_obj->rank = $card_obj->rank==1 ? 0:1;
//        if($card_obj->save()){
//            //查询之前置顶的帖子
//            $this->success('操作成功',url('basketball/content/card_list',['id'=>$bbs_id]));
//        }else{
//            $this->error('操作失败',url('basketball/content/card_list',['id'=>$bbs_id]));
//
//        }
//    }
//



//    //论坛轮播图
//    public function forum_map(){
//
//        return view();
//    }
//
//    //论坛评论管理
//    public function forum_review(){
//        $type = \request()->get('type');
//        $keys = \request()->get('keys');
//        if(empty($type)||$type==1){
//            //查询所有评论内容
//            $comment_list = CardComment::where('dstatus',1)->with(['getUser'=>function($query){
//                $query->field('id,username,phone');
//            }])->with(['getCard'=>function($query2){
//                $query2->field('id,title');
//            }])->with(['getBbs'=>function($query3){
//                $query3->field('id,title');
//            }])->order('id desc')->paginate(10);
//        }else{
//
//        }
//        return view('',['comment_list'=>$comment_list,'type'=>$type,'keys'=>$keys]);
//
//    }




    //资讯列表
    public function information(){
        //获取筛选条件
        $title = request()->get('title');
        if(empty($title)){
            $title = '';
        }
        $info_list = Information::where('dstatus','<>',3)->where('title','like','%'.$title.'%')->order('id desc')->paginate(10,false,['query'=>['title'=>$title]]);
        return view('',['info_list'=>$info_list,'title'=>$title]);
    }

    //添加资讯
    public function information_add(){
        if(request()->isPost()){
            $data = request()->post();
            $url = request()->post('url');
            if($url){
                //判断是否有视频地址传入,有则为视频资讯
                $data['url'] = $url;
                $data['type'] = 3;
            }
            //获取封面图
            $pic_h = uploadImageOne('pic_h');
            $pic_h2 = uploadImageOne('pic_h2');
            $pic_h3 = uploadImageOne('pic_h3');
            //判断是否有三张图片上传,有则为图片资讯
            if($pic_h['status']==0 && $pic_h2['status']==0 && $pic_h3['status']==0){
                $data['pic_h'] = $pic_h['message'];
                $data['pic_h2'] = $pic_h2['message'];
                $data['pic_h3'] = $pic_h3['message'];
                $data['type'] = 1;
            }

            if($pic_h['status']==0 && $pic_h2['status']!==0 && $pic_h3['status']!==0 && empty($url)){
                $data['pic_h'] = $pic_h['message'];
                $data['type'] = 2;
            }

            //插入数据到咨询表
            $information = Information::create($data);
            if($information->id){
                $this->success('添加成功','basketball/content/information');
            }else{
                $this->success('添加失败','basketball/content/information');
            }
        }
        return view();
    }

    /**
     * 修改资讯
     */
    public function information_edit(){
        $id = request()->get('id');
        $info_list = Information::get($id);
            if(request()->isPost()){
            $data = request()->post();
            $url = request()->post('url');
            if($url){
                //判断是否有视频地址传入,有则为视频资讯
                $data['url'] = $url;
                $data['type'] = 3;
            }
            //获取封面图
            $pic_h = uploadImageOne('pic_h');
            $pic_h2 = uploadImageOne('pic_h2');
            $pic_h3 = uploadImageOne('pic_h3');
            //判断是否有三张图片上传,有则为图片资讯
            if($pic_h['status']==0 && $pic_h2['status']==0 && $pic_h3['status']==0){
                $data['pic_h'] = $pic_h['message'];
                $data['pic_h2'] = $pic_h2['message'];
                $data['pic_h3'] = $pic_h3['message'];
                $data['type'] = 1;
            }

            if($pic_h['status']==0 && $pic_h2['status']!==0 && $pic_h3['status']!==0 && empty($url)){
                $data['pic_h'] = $pic_h['message'];
                $data['type'] = 2;
            }
            //插入数据
            $video = $info_list->save($data,['id' => $id]);
            if($video){
                $this->success('修改成功','basketball/content/information');
            }else{
                $this->error('修改失败','basketball/content/information');
            }
        }
        return view('',['info_list'=>$info_list]);
    }


    //视频管理
    public function video(){
        $arrWhere = [];
        $title = request()->get('title');
        $status = request()->get('status');
        $title_three = request()->get('title_three');
        if(!empty($title)){
            $arrWhere['title'] = $title;
        }
        if(!empty($status)){
            $arrWhere['dstatus'] = $status;
        }
        if(empty($title_three)){
            $title_three = '';
        }
        $video_list = CollectionM::where('dstatus','<>',3)->where('title_three','like','%'.$title_three.'%')->where($arrWhere)->order('id','desc')->paginate(10,false,[ 'query' => $arrWhere]);
        //获取分页显示
        return view('',['video_list'=>$video_list,'title'=>$title,'status'=>$status,'title_three'=>$title_three]);
    }

    //修改视频
    public function video_edit(){
        $id = request()->get('id');
        $video_list = CollectionM::get($id);
        if(request()->isPost()){
            $data = request()->post();
            $img = uploadImageOne('picture_h');
            if($img['status']==0){
                $data['picture_h'] = $img['message'];
            }
            $video = $video_list->save($data,['id' => $id]);
            if($video){
                $this->success('修改成功','basketball/content/video');
            }else{
                $this->error('修改失败','basketball/content/video');
            }
        }
        return view('',['video_list'=>$video_list]);
    }


    //添加视频
    public function video_add(){
        if(request()->isPost()){
            $data = request()->post();
            $img = uploadImageOne('picture_h');
            if($img['status']==0){
                $data['picture_h'] = $img['message'];
            }
            $status = CollectionM::create($data);
            if($status){
                $this->success('添加成功','basketball/content/video');
            }else{
                $this->error('添加失败','basketball/content/video');
            }
        }
        return view();
    }

    /**
     *  篮球视频推荐
     */
    public function recommend(){
        $id = \request()->get('id');

        $obj = CollectionM::get($id);
        if($obj->is_recommend==1){
            $obj->is_recommend = 0;
        }else{
            $obj->is_recommend = 1;
        }
        $res = $obj->save();
        if($res){
            $this->success('操作成功',url('basketball/content/video'));
        }else{
            $this->error('操作失败','basketball/content/video');
        }
    }


    /**
     * @内容管理统一屏蔽
     */
    public function shield(){
        $id = request()->get('id');
        $type = request()->get('type');
        $bbs_id = request()->get('bbs_id');
        if($type=='information'){
            $obj = Information::get($id);
        }
        if($type=='video'){
            $obj = CollectionM::get($id);
        }
        if($type=='forum_list'){
            $obj = BbsMain::get($id);
        }
        if($type=='banner_list'){
            $obj = Banner::get($id);
        }
        if($obj->dstatus==2){
            $obj->dstatus = 1;
        }else{
            $obj->dstatus = 2;
        }
        $res = $obj->save();
        if($res){
            $this->success('操作成功',url('basketball/content/'.$type,['id'=>$id]));
        }else{
            $this->error('操作失败','basketball/content/'.$type);
        }
    }

    /**
     * 内容管理统一删除
     */
    public function del(){
        $id = request()->get('id');
        $type = request()->get('type');
        $bbs_id = request()->get('bbs_id');
        if($type=='information'){
            $obj = Information::get($id);
        }
        if($type=='video'){
            $obj = CollectionM::get($id);
        }
        if($type=='forum_list'){
            $obj = BbsMain::get($id);
        }
        if($type=='card_list'){
            $obj = BbsCard::get($id);
        }
        if($type=='banner_list'){
            $obj = Banner::get($id);
        }
        $obj->dstatus = 3;
        $res = $obj->save();
        if($res){
            $this->success('删除成功',url('basketball/content/'.$type,['id'=>$bbs_id]));
        }else{
            $this->error('删除失败',url('basketball/content/'.$type,['id'=>$bbs_id]));
        }
    }

}